package com.sr.examination.category.wrong;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import com.sr.examination.R;
import com.sr.examination.common.event.CollectionEvent;
import com.sr.examination.exam.exercise.ExerciseActivity;
import com.sr.examination.net.Net;
import com.tl.commonlibrary.event.EventUtil;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseListViewFragmentActivity;
import com.tl.commonlibrary.ui.adapter.ViewHolder;
import com.tl.commonlibrary.ui.beans.CollectionBean;
import com.tl.commonlibrary.ui.beans.ExerciseListBean;
import com.tl.commonlibrary.ui.beans.WrongBean;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import retrofit2.Call;

/**
 * Created by jiangzhongshan on 18/2/4.
 * 错题
 */

public class WrongActivity extends BaseListViewFragmentActivity<WrongBean> {
    public static void start(Context context) {
        Intent starter = new Intent(context, WrongActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventUtil.register(this);
        setTitle(getString(R.string.exam_wrong_answer));
        setListViewPadding(0, (int) getResources().getDimension(R.dimen.base_spacing_20px), 0, 0);
        setListDividerHeightTransparent((int) getResources().getDimension(R.dimen.divider_height));
        enablePull(true, false);
        autoRefresh();
    }

    /**
     * list item的布局文件
     *
     * @return
     */
    @Override
    public int getItemLayoutResId() {
        return R.layout.item_wrong;
    }

    /**
     * 数据请求
     *
     * @param isRefresh
     */
    @Override
    public void requestData(final boolean isRefresh) {
        dismissNoData();
        Net.getWrongList(new RequestListener<BaseBean<ArrayList<WrongBean>>>() {
            @Override
            public void onSuccess(Call<BaseBean<ArrayList<WrongBean>>> call, BaseBean<ArrayList<WrongBean>> response) {
                if (response.isSuccessful()) {
                    datas = response.data;
                    if (isRefresh) {
                        if (datas == null || datas.isEmpty()) {
                            listView.setAdapter(null);
                            showNoDataView(getString(R.string.no_data_exercise_wrong_list));
                        } else {
                            setDatas(datas);
                        }
                    } else {
                        addDatas(datas);
                    }
                }
                requestSuccess();
            }

            @Override
            public void onFailed(Call<BaseBean<ArrayList<WrongBean>>> call, ErrorResponse errorResponse) {
                requestFailed();
            }
        });
    }

    /**
     * 点击了item
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (adapter != null) {
            WrongBean bean = adapter.getItem(position);
            if (bean != null) {
                ExerciseActivity.startForWrongList(context, bean.getPaperTitle(), bean.getPaperId(), bean.getPUserSummaryId());
            }
        }
    }

    /**
     * 点击了item中某一个子控件，
     *
     * @param v
     */
    @Override
    public void onClickViewInListItem(View v) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(CollectionEvent collectionEvent) {
        if (controller != null && !controller.isRefreshing()) {
            controller.autoRefresh();
        }
    }

    /**
     * Adapter的getView
     *
     * @param viewHolder
     * @param entity
     * @param position
     * @param convertView
     * @param isConvert
     * @param itemViewType
     * @param parent
     */
    @Override
    public void getView(ViewHolder viewHolder, WrongBean entity, int position, View convertView, boolean isConvert, int itemViewType, ViewGroup parent) {
        TextView titleTView = viewHolder.getView(R.id.titleTView);
        TextView collectionCountTView = viewHolder.getView(R.id.wrongCountTView);
        titleTView.setText(entity.getPaperTitle());
        collectionCountTView.setText(getString(R.string.wrong_count, entity.getWrongItemTotal()));
    }

    @Override
    public void onNoDataClicked() {
        super.onNoDataClicked();
        controller.autoRefresh();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onFragmentTrigger(int tag, Bundle bundle) {

    }
}
